import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'

// reducer     用来处理业务逻辑
// initState   初始值

const reducer = (state, action) => {
    // reducer函数中有两个默认参数
    const NewState = JSON.parse(JSON.stringify(state))
    switch(action.type) {
        case "CHANGE_NAME":
            NewState.name = "张三"
            return NewState
        case "INIT_LIST":
            NewState.list = action.data
            return NewState
        default:
            return state
    }
}

const initState = {
    name: "王耀",
    age: 30,
    list: []
}
const store = createStore(reducer, initState, applyMiddleware(thunk))

// store.getState   读取的是reducer函数中的返回值
// store.subscribe  当redux中的数据发生修改的时候会触发subscribe

export default store